package com.rx.mgt.controller;

import com.rx.mgt.domain.BlackListDomain;
import com.rx.mgt.domain.WhiteListDomain;
import com.rx.mgt.service.EmailBlackListService;
import com.rx.mgt.service.EmailWhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * Created by zxj on 2017/8/15.
 */
@Controller
public class EmailBlackListAndWidthListController {


    @Autowired
    private EmailBlackListService blackListService;
    @Autowired
    private EmailWhiteListService whiteListService;


    /**
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryBlackList.htmlx")
    public String queryBlackList(HttpServletRequest request){
        List<BlackListDomain> list;
        list = blackListService.queryBlackList();
        request.setAttribute("list",list);
        return "sys_black";
    }


    @RequestMapping("/queryWhiteList.htmlx")
    public String queryWhiteList(HttpServletRequest request){
        List<WhiteListDomain> list;
        list = whiteListService.queryBlackList();
        request.setAttribute("list",list);
        return "sys_white";
    }
    /**
     * 增加黑名单
     */
    @RequestMapping("/addBlackList.htmlx")
    @ResponseBody
    public String addBlackList(@RequestParam("address")String address){
        int i = blackListService.addBlackList(address);
        if(i==1){
            return "error";
        }else if(i==2){
            return "reman";
        }
        return "success";

    }
    /**
     * 添加白名单
     */
    @RequestMapping("/addWhiteList.htmlx")
    @ResponseBody
    public String addWhiteList(@RequestParam("address")String address){
        int i = whiteListService.addWhiteList(address);
        if(i==1){
            return "error";
        }else if(i==2){
            return "reman";
        }
        return "success";

    }

    /**
     * 删除黑名DNA
     */
    @RequestMapping("/deleteBlackList.htmlx")
    @ResponseBody
    public String deleteBlackList(@RequestParam("ids")String ids){
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id:idList){
            blackListService.deleteBlackList(id);
        }
        return "success";
    }

    /**
     * 删除白名单
     */
    @RequestMapping("/deleteWhiteList.htmlx")
    @ResponseBody
    public String deleteWhiteList(@RequestParam("ids")String ids){
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id:idList){
            whiteListService.deleteWhiteList(id);
        }
        return "success";
    }

    /**
     * 禁用和启用黑名单
     */
    @RequestMapping("/upateBlackList.htmlx")
    @ResponseBody
    public String upateBlackList(@RequestParam("ids")String ids,@RequestParam("flag")String flag){
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id:idList){
            blackListService.upateBlackList(id,flag);
        }
        return "success";
    }



    /**
     * 禁用和启用白名单
     */
    @RequestMapping("/upateWhiteList.htmlx")
    @ResponseBody
    public String upateWhiteList(@RequestParam("ids")String ids,@RequestParam("flag")String flag){
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id:idList){
            whiteListService.upateWhiteList(id,flag);
        }
        return "success";
    }


    /**
     *根据地址删除白名单
     */
    @RequestMapping("/deleteWhiteListByAddress.htmlx")
    @ResponseBody
    public String deleteWhiteListByAddress(@RequestParam("address")String address){
            whiteListService.deleteWhiteListByAddress(address);
        return "success";

    }

    /**
     * 根据地址删除黑名单
     * @param address
     * @return
     */

    @RequestMapping("/deleteBlackListByAddress.htmlx")
    @ResponseBody
    public String deleteBlackListByAddress(@RequestParam("address")String address){
            blackListService.deleteBlackListByAddress(address);
        return "success";

    }

}
